package com.ruoyi.equipment.apiController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.equipment.domain.TDrawer;
import com.ruoyi.equipment.domain.TEquipment;
import com.ruoyi.equipment.enums.QosEnum;
import com.ruoyi.equipment.mapper.TDrawerMapper;
import com.ruoyi.equipment.mapper.TEquipmentMapper;
import com.ruoyi.equipment.util.EmqxUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mqtt")
public class HttpAuthController {

    @Autowired
    private TEquipmentMapper tEquipmentMapper;

    @Autowired
    private TDrawerMapper tDrawerMapper;

    //验证设备是否可以连接
    @PostMapping("/auth")
    @Transactional
    public ResponseEntity auth(String clientid, String username, String password) {
        if (username.equals("admin") && password.equals("public")) {
            int i = tEquipmentMapper.selectOne(clientid);
            //设备从未连接过
            if (i == 0) {
                return new ResponseEntity(HttpStatus.OK);
            }
            //设备连接过 查看设备是否可以上线
            if (i == 1) {
                //检查设备是否是被下线 状态是否改变 根据id 号获取某一详细信息
                TEquipment tEquipment = tEquipmentMapper.selectTEquipmentByEquipmentId(clientid);
                if (tEquipment.getEquipmentStu() == 1) {
                    return new ResponseEntity(HttpStatus.UNAUTHORIZED);
                }
                //改变设备的状态
//                tEquipment.setStatus(0);
//                tEquipmentMapper.updateTEquipment(tEquipment);
                return new ResponseEntity(HttpStatus.OK);
            }

        }
        return new ResponseEntity(HttpStatus.UNAUTHORIZED);
    }

    @PostMapping("/superuser")
    public ResponseEntity supper(String username) {
        //通过username 进行一个检索

        if (username.equals("admin")) {
            return new ResponseEntity(HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.UNAUTHORIZED);

    }

    @PostMapping("/acl")
    public ResponseEntity acl(int access, String username, String clientid, String topic) {
        //
        if ("admin".equals(username)) {
            return new ResponseEntity(HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.UNAUTHORIZED);
    }

    //钩子方法
    @PostMapping("/webhook")
    @Transactional(rollbackFor = Exception.class)
    public void hook(@RequestBody Map<String, Object> params) throws InterruptedException {
        String action = (String) params.get("action");
        String clientId = (String) params.get("clientid");

        if (action.equals("client_connected")) {
            int i = tEquipmentMapper.selectOne(clientId);
            //设备从未连接过
            if (i == 0) {
                //添加设备 并修改设备状态
                TEquipment tEquipment = new TEquipment();
                tEquipment.setMagazineNum(8);
                tEquipment.setCreatTime(new Date());
                tEquipment.setStatus(0);
                tEquipment.setEquipmentStu(0);
                tEquipment.setEquipmentId(clientId);
                tEquipment.setEquipmentDeploy(0);
                tEquipmentMapper.insertTEquipment(tEquipment);
                //添加仓匣 8 个循环添加
                List<TDrawer> tDrawerList = new ArrayList<>();
                for (int j = 0; j < 8; j++) {
                    TDrawer tDrawer = new TDrawer();
                    tDrawer.setDrawerNo(j + 1);
                    tDrawer.setEquipmentId(clientId);
                    tDrawer.setDrawerStatus(0);
                    tDrawer.setDrawerStu(1);
                    tDrawerList.add(tDrawer);
                }
                tDrawerMapper.forInsertDrawer(tDrawerList);
            }
            //设备连接过 查看设备是否可以上线
            if (i == 1) {
                //检查设备是否是被下线 状态是否改变 根据id 号获取某一详细信息
                TEquipment tEquipment = tEquipmentMapper.selectTEquipmentByEquipmentId(clientId);
                //改变设备的状态
                tEquipment.setStatus(0);
                tEquipmentMapper.updateTEquipment(tEquipment);
            }
        }
        //当设备断开的时候进行一个自动改变设备状态
        if (action.equals("client_disconnected")) {
            //改变设备的状态
            TEquipment tEquipment = new TEquipment();
            tEquipment.setEquipmentId(clientId);
            tEquipment.setStatus(1);
            tEquipmentMapper.updateTEquipment(tEquipment);
        }
    }

}
